﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.MenuCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.MenuHandler
{
    public class AddRoleMenuHandler : IRequestHandler<AddRoleMenuCommand, APIResult<object>>
    {
        private readonly IBaseRepository<RoleMenu> _baseRepository;
        private readonly IMapper _mapper;

        public AddRoleMenuHandler(IBaseRepository<RoleMenu> baseRepository, IMapper mapper)
        {
            _baseRepository = baseRepository;
            _mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(AddRoleMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            foreach (var item in request.MenuId)
            {
                var ur = new RoleMenu();
                ur.MenuId = item;
                ur.RoleId = request.RoleId;
                _baseRepository.Add(ur);
            }


            result.Code = APIEnum.Success;
            result.Msg = "添加角色菜单关系表成功";

            return await Task.FromResult(result);
        }
    }
}
